package com.yinhe.util.jibeiSync;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DateTimeUtil
 * @Description
 * @Author xiaolongZzz66
 * @Time 2023/12/5 11:03
 * @Version 1.0
 */
public class DateTimeUtil {


    private static final String ISO8601 = "yyyy_MM_dd'T'HH_mm_ss";

    public static final String WIND_SOLAR_TIME = "yyyyMMdd-HHmm";
    public static final String WIND_SOLAR_TIME_10DAY = "yyyyMMdd";

    public static final String StandTime = "yyyy-MM-dd HH:mm:ss";
    public static final String STANDDATE = "yyyy-MM-dd";

    private static final String Asia_Shanghai = "Asia/Shanghai";




    public static String IsoStringToStatandLocateDate(String timeStr) {
        if( timeStr.isEmpty()) {
            return null;
        }
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(ISO8601);
        LocalDateTime dateTime = LocalDateTime.parse(timeStr, inputFormatter);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(StandTime);
        String output = dateTime.format(outputFormatter);
        // LocalDateTime outputTime = LocalDateTime.parse(output, outputFormatter);

        return output;
    }

    /**
     * iso标准字符串转为LocalDateTime
     * @param timeStr
     * @return
     */
    public static LocalDateTime IsoStringToLocateDate(String timeStr) {
        if( timeStr.isEmpty()) {
            return null;
        }
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(ISO8601);
        LocalDateTime dateTime = LocalDateTime.parse(timeStr, inputFormatter);

        return dateTime;
    }

    public static LocalDateTime StringToLocateDate(String timeStr) {
        if( timeStr.isEmpty()) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(StandTime);
        return LocalDateTime.parse(timeStr,formatter);
    }

    public static String formatISO8601(LocalDateTime localDateTime) {
        //转为ISO8601标准
        ZonedDateTime zdt = ZonedDateTime.of(localDateTime, ZoneId.of(Asia_Shanghai));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ISO8601);
        return zdt.format(formatter);
    }

    public static String format(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    public static String formatLocaldate(LocalDate localDate, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(formatter);
    }


    /**
     * 根据时间获取15分钟后ISO8601标准时间字符串
     * @param time
     * @return
     */
    public static String getIso8601NameByLocateDate(LocalDateTime time){
        LocalDateTime localDateTime = time.plusMinutes(15);
        return formatISO8601(localDateTime);
    }


    /**
     * 获取今天的96时刻值
     * @return
     */
    public static List<String> getToday96Point(){
        LocalDateTime today0H0M0S = getToday0H0M0S();
        List<String> point96 = new ArrayList<>();

        for (int i = 0; i < 96; i++) {
            int addMinu = i * 15;
            LocalDateTime itemMinu = today0H0M0S.plusMinutes(addMinu);
            point96.add(formatISO8601(itemMinu));
            // System.out.println("96时刻值："+ formatISO8601(itemMinu));
            // System.out.println("长度"+ point96.size());
        }
        return point96;
    }

    // 获取风力和光伏发电的96刻度值
    public static List<String> getWindSolarToday96Point(){
        LocalDateTime today0H0M0S = getToday0H0M0S();
        List<String> point96 = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(WIND_SOLAR_TIME);

        for (int i = 0; i < 96; i++) {
            int addMinu = i * 15;
            LocalDateTime itemMinu = today0H0M0S.plusMinutes(addMinu);
            //转为ISO8601标准
            ZonedDateTime zdt = ZonedDateTime.of(itemMinu, ZoneId.of(Asia_Shanghai));
            String format = zdt.format(formatter);
            point96.add(format);
            // System.out.println(format);
        }
        return point96;
    }


    public static LocalDateTime windSolarStringToLocalDT(String time){
        if (time.isEmpty()) return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(WIND_SOLAR_TIME);
        LocalDateTime dateTime = LocalDateTime.parse(time, dateTimeFormatter);
        return dateTime;
    }

    public static LocalDateTime windSolarStringTo10dayLocalDT(String time){
        if (time.isEmpty()) return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(WIND_SOLAR_TIME_10DAY);
        LocalDate localDate = LocalDate.parse(time, dateTimeFormatter);
        LocalDateTime dateTime = localDate.atTime(00, 00, 00);
        return dateTime;
    }


    /**
     * 当前系统0点0分0秒时间
     * @return
     */
    public static LocalDateTime getToday0H0M0S(){
        LocalDateTime now = LocalDateTime.now(); // 获取当前系统时间
        ZonedDateTime zdt = now.atZone(ZoneId.of(Asia_Shanghai)); // 将当前系统时间转换为带时区的日期时间

        LocalDateTime zeroTime = LocalDateTime.of(zdt.toLocalDate(), LocalTime.of(0, 0, 0)); // 创建0点0分0秒的日期时间对象
        // System.out.println("当前系统时间的0点0分0秒值: " + zeroTime);
        return zeroTime;
    }

    // 当期系统日期前10天
    public static LocalDate getTodayBefore10d(){
        LocalDateTime today0H0M0S = getToday0H0M0S();
        LocalDateTime localDateTime = today0H0M0S.plusDays(-7);
        // String date = localDateTime.format(DateTimeFormatter.ofPattern("yyyy_MM_dd"));
        return localDateTime.toLocalDate();
    }

    public static void main(String[] args) {
        LocalDateTime today0H0M0S = getToday0H0M0S();
        System.out.println(today0H0M0S);


        // getToday96Point();
        // trimString("@ Valid    ITF_ID   ITF_Name Fault_ID Fault_Name  Pcut  Pstc");

        // LocalDateTime lastSyncTime =  StringToLocateDate("2023-12-01 11:15:00");
        // String lastSyncTimeStr = formatISO8601(lastSyncTime);
        // String iso8601NameByLocateDate = getIso8601NameByLocateDate(lastSyncTime);
        //
        // String abc = "aa   b c   d e ff  g l mm ";
        // String s2 = abc.replaceAll("\\s+", " ");
        // String[] s = s2.split(" ");
        // for (String s1 : s) {
        //     System.out.println(s1);
        // }
        //
        //
        // System.out.println(iso8601NameByLocateDate);
        // System.out.println(lastSyncTimeStr);

        // String a = "2023_11_29T15_00_00";
        // System.out.println(a.substring(0, 16));
    }
}
